package liumengbaio.lolzhangyoubao.Activity.Activity;

import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.squareup.picasso.Picasso;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import de.hdodenhof.circleimageview.CircleImageView;
import liumengbaio.lolzhangyoubao.Activity.Fragment.FragmentCommunity;
import liumengbaio.lolzhangyoubao.Activity.Fragment.FragmentHero;
import liumengbaio.lolzhangyoubao.Activity.Fragment.FragmentInfo;
import liumengbaio.lolzhangyoubao.Activity.Fragment.FragmentMore;
import liumengbaio.lolzhangyoubao.Activity.Fragment.FragmentVideo;
import liumengbaio.lolzhangyoubao.Activity.InFo.UserInfo;
import liumengbaio.lolzhangyoubao.Activity.View.SaveDialog;
import liumengbaio.lolzhangyoubao.R;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    private Button btn_info, btn_hero, btn_video, btn_community, btn_more;
    private Button left_title, right_title_down, right_title, btn_setting, btn_go_out;
    private Button menu_icon_wallet, menu_icon_message, menu_icon_fav, menu_icon_coment, menu_icon_comunity,
            menu_icon_vedio, menu_icon_task, menu_icon_geuss, btn_night;
    private TextView middle_title, menu_user_name_login, tv_setting, tv_night;
    private CircleImageView head_icon;
    private FragmentInfo info;
    private FragmentHero hero;
    private FragmentVideo video;
    private FragmentCommunity community;
    private FragmentMore more;
    private UserInfo user_information;
    private FrameLayout container;
    private Context mContext;
    private SharedPreferences sp;
    private SharedPreferences.Editor editor;
    private android.support.v4.app.FragmentManager fm = getSupportFragmentManager();
    private android.support.v4.app.FragmentTransaction transaction;
    private com.jeremyfeinstein.slidingmenu.lib.SlidingMenu menu;
    private boolean flag;
    private LinearLayout gellery_line, fans_line, dongtai_line, look_line;
    private RelativeLayout main_night_mode, menu_night_mode;
    private boolean night_mode = false;
    private String url = "http://lol.zhangyoubao.com/apis/rest/UsersService/getU" +
            "serInfo?token=1285c08b612411d0bfc35f213eb5d92a73&userid=15863305&i" +
            "_=UcDAihrd4WMeNwR+vF4c+jtL6gm77orVBqYdtvp2Sis=&deviceId=UcDAihrd4" +
            "WMeNwR+vF4c+jtL6gm77orVBqYdtvp2Sis=&t_=1472612822977&p_=11276&v_=" +
            "400901&a_=lol&pkg_=com.anzogame.lol&d_=android&osv_=17&cha=baiduM" +
            "artket&u_=15863305&modle_=vivo%20Y15T&api=UsersService.getUserInfo" +
            "&time=1472612822&game=lol&secretId=AKIDz8krbsJ5yKBZQpn74WFk" +
            "mLPx3gnPhESA&secretVersion=v1.0&nonce=21099&secretSignature=hsCtx" +
            "a2KTJoGQPjNn9MCbcrGp80=";

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        EventBus.getDefault().register(this);
        sp = getSharedPreferences("userInfo", MODE_PRIVATE);
        flag = sp.getBoolean("flag", false);
        Log.i("flag", "" + flag);
        editor = sp.edit();
        initSlidingMenu();
        iniView();
        initData();
    }

    private void initSlidingMenu() {
        // configure the SlidingMenu
        menu = new com.jeremyfeinstein.slidingmenu.lib.SlidingMenu(MainActivity.this);
        menu.setMode(com.jeremyfeinstein.slidingmenu.lib.SlidingMenu.LEFT);
        // 设置触摸屏幕的模式
        menu.setTouchModeAbove(com.jeremyfeinstein.slidingmenu.lib.SlidingMenu.TOUCHMODE_MARGIN);
        menu.setShadowWidthRes(R.dimen.shadow_width);

        // 设置滑动菜单视图的宽度
        menu.setBehindOffsetRes(R.dimen.slidingmenu_offset);
        // 设置渐入渐出效果的值
        menu.setFadeDegree(0.35f);
        /**
         * SLIDING_WINDOW will include the Title/ActionBar in the content
         * section of the SlidingMenu, while SLIDING_CONTENT does not.
         */
        menu.attachToActivity(this, com.jeremyfeinstein.slidingmenu.lib.SlidingMenu.SLIDING_WINDOW);

        View view = LayoutInflater.from(this).inflate(R.layout.activity_menu, null);
        view_setonclick(view);

        //为侧滑菜单设置布局
        menu.setMenu(view);

    }

    private void view_setonclick(View v) {
        head_icon = (CircleImageView) v.findViewById(R.id.head_icon);
        tv_night = (TextView) v.findViewById(R.id.tv_night);
        tv_night.setOnClickListener(this);
        btn_night = (Button) v.findViewById(R.id.btn_night);
        btn_night.setOnClickListener(this);
        menu_night_mode = (RelativeLayout) v.findViewById(R.id.menu_night_mode);
        if (flag)
            Picasso.with(MainActivity.this).load(sp.getString("headIcon", "")).into(head_icon);
        menu_user_name_login = (TextView) v.findViewById(R.id.menu_user_name_login);
        menu_icon_wallet = (Button) v.findViewById(R.id.menu_icon_wallet);
        menu_icon_message = (Button) v.findViewById(R.id.menu_icon_message);
        menu_icon_fav = (Button) v.findViewById(R.id.menu_icon_fav);
        menu_icon_coment = (Button) v.findViewById(R.id.menu_icon_coment);
        menu_icon_comunity = (Button) v.findViewById(R.id.menu_icon_comunity);
        menu_icon_vedio = (Button) v.findViewById(R.id.menu_icon_vedio);
        menu_icon_task = (Button) v.findViewById(R.id.menu_icon_task);
        menu_icon_geuss = (Button) v.findViewById(R.id.menu_icon_geuss);
        gellery_line = (LinearLayout) v.findViewById(R.id.gellery_line);
        dongtai_line = (LinearLayout) v.findViewById(R.id.dongtai_line);
        fans_line = (LinearLayout) v.findViewById(R.id.fans_line);
        look_line = (LinearLayout) v.findViewById(R.id.look_line);
        tv_setting = (TextView) v.findViewById(R.id.tv_setting);
        btn_go_out = (Button) v.findViewById(R.id.btn_go_tou);
        btn_setting = (Button) v.findViewById(R.id.btn_setting);
        menu_user_name_login.setOnClickListener(this);
        btn_setting.setOnClickListener(this);
        btn_go_out.setOnClickListener(this);
        tv_setting.setOnClickListener(this);
        gellery_line.setOnClickListener(this);
        dongtai_line.setOnClickListener(this);
        look_line.setOnClickListener(this);
        fans_line.setOnClickListener(this);
        head_icon.setOnClickListener(this);
        menu_icon_wallet.setOnClickListener(this);
        menu_icon_message.setOnClickListener(this);
        menu_icon_fav.setOnClickListener(this);
        menu_icon_coment.setOnClickListener(this);
        menu_icon_comunity.setOnClickListener(this);
        menu_icon_vedio.setOnClickListener(this);
        menu_icon_task.setOnClickListener(this);
        menu_icon_geuss.setOnClickListener(this);
    }

    private void initData() {
        left_title.setOnClickListener(this);
        info = new FragmentInfo();
        video = new FragmentVideo();
        hero = new FragmentHero();
        community = new FragmentCommunity();
        more = new FragmentMore();
        transaction = fm.beginTransaction();
        transaction.add(R.id.mine_FramLayout, info);
        transaction.add(R.id.mine_FramLayout, video);
        transaction.add(R.id.mine_FramLayout, community);
        transaction.add(R.id.mine_FramLayout, more);
        transaction.add(R.id.mine_FramLayout, hero);
        transaction.hide(hero);
        transaction.hide(video);
        transaction.hide(community);
        transaction.hide(more);
        transaction.commit();
    }

    private void iniView() {
        btn_community = (Button) findViewById(R.id.btn_community);
        main_night_mode = (RelativeLayout) findViewById(R.id.main_night_mode);
        btn_hero = (Button) findViewById(R.id.btn_hero);
        btn_info = (Button) findViewById(R.id.btn_home);
        btn_video = (Button) findViewById(R.id.btn_video);
        btn_more = (Button) findViewById(R.id.btn_more);
        left_title = (Button) findViewById(R.id.left_title);
        middle_title = (TextView) findViewById(R.id.title);
        right_title = (Button) findViewById(R.id.right_title);
        right_title_down = (Button) findViewById(R.id.right_title_down);
        container = (FrameLayout) findViewById(R.id.mine_FramLayout);
    }

    public void Home(View view) {
        middle_title.setText("新闻资讯");
        right_title.setVisibility(View.VISIBLE);
        right_title_down.setVisibility(View.INVISIBLE);
        btn_video.setBackgroundResource(R.drawable.home_video);
        btn_hero.setBackgroundResource(R.drawable.home_hero);
        btn_community.setBackgroundResource(R.drawable.home_community);
        btn_more.setBackgroundResource(R.drawable.home_more);
        btn_info.setBackgroundResource(R.drawable.home_information_);
        android.support.v4.app.FragmentTransaction home_fragment = fm.beginTransaction();
        home_fragment.hide(hero);
        home_fragment.hide(video);
        home_fragment.hide(community);
        home_fragment.hide(more);
        home_fragment.show(info);
        home_fragment.commit();

    }

    public void Video(View view) {
        middle_title.setText("游戏视频");
        right_title.setVisibility(View.INVISIBLE);
        right_title_down.setVisibility(View.VISIBLE);
        btn_video.setBackgroundResource(R.drawable.home_video_);
        btn_hero.setBackgroundResource(R.drawable.home_hero);
        btn_community.setBackgroundResource(R.drawable.home_community);
        btn_more.setBackgroundResource(R.drawable.home_more);
        btn_info.setBackgroundResource(R.drawable.home_information);
        android.support.v4.app.FragmentTransaction vedio_fragment = fm.beginTransaction();
        vedio_fragment.hide(hero);
        vedio_fragment.hide(community);
        vedio_fragment.hide(more);
        vedio_fragment.hide(info);
        vedio_fragment.show(video);
        vedio_fragment.commit();

    }

    public void Hero(View view) {
        middle_title.setText("英雄列表");
        right_title.setVisibility(View.INVISIBLE);
        right_title_down.setVisibility(View.INVISIBLE);
        btn_video.setBackgroundResource(R.drawable.home_video);
        btn_hero.setBackgroundResource(R.drawable.home_hero_);
        btn_community.setBackgroundResource(R.drawable.home_community);
        btn_more.setBackgroundResource(R.drawable.home_more);
        btn_info.setBackgroundResource(R.drawable.home_information);
        android.support.v4.app.FragmentTransaction hero_fragment = fm.beginTransaction();
        hero_fragment.show(hero);
        hero_fragment.hide(video);
        hero_fragment.hide(community);
        hero_fragment.hide(more);
        hero_fragment.hide(info);
        hero_fragment.commit();

    }

    public void Community(View view) {
        middle_title.setText("社区");
        right_title.setVisibility(View.INVISIBLE);
        right_title_down.setVisibility(View.INVISIBLE);
        btn_video.setBackgroundResource(R.drawable.home_video);
        btn_hero.setBackgroundResource(R.drawable.home_hero);
        btn_more.setBackgroundResource(R.drawable.home_more);
        btn_info.setBackgroundResource(R.drawable.home_information);
        btn_community.setBackgroundResource(R.drawable.home_community_);
        android.support.v4.app.FragmentTransaction community_fragment = fm.beginTransaction();
        community_fragment.hide(hero);
        community_fragment.hide(video);
        community_fragment.show(community);
        community_fragment.hide(more);
        community_fragment.hide(info);
        community_fragment.commit();

    }

    public void More(View view) {
        middle_title.setText("更多");
        right_title.setVisibility(View.INVISIBLE);
        right_title_down.setVisibility(View.INVISIBLE);
        btn_video.setBackgroundResource(R.drawable.home_video);
        btn_hero.setBackgroundResource(R.drawable.home_hero);
        btn_community.setBackgroundResource(R.drawable.home_community);
        btn_info.setBackgroundResource(R.drawable.home_information);
        btn_more.setBackgroundResource(R.drawable.home_more_);
        android.support.v4.app.FragmentTransaction more_fragment = fm.beginTransaction();
        more_fragment.hide(hero);
        more_fragment.hide(video);
        more_fragment.show(more);
        more_fragment.hide(community);
        more_fragment.hide(info);
        more_fragment.commit();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.menu_user_name_login:
                if (flag) {
                    menu_user_name_login.setBackgroundResource(R.mipmap.bg);
                    menu_user_name_login.setText("");
                } else {
                    menu_user_name_login.setBackgroundColor(Color.TRANSPARENT);
                    menu_user_name_login.setText("");
                }
                break;
            case R.id.btn_night:
            case R.id.tv_night:
                if (night_mode) {
                    menu_night_mode.setBackgroundColor(Color.TRANSPARENT);
                    main_night_mode.setBackgroundColor(Color.TRANSPARENT);
                    night_mode = false;
                } else {
                    menu_night_mode.setBackgroundColor(Color.parseColor("#99020639"));
                    main_night_mode.setBackgroundColor(Color.parseColor("#99020639"));
                    night_mode = true;
                }
                break;
            case R.id.left_title:
                menu.toggle();
                break;
            case R.id.head_icon:
                if (flag) {
                    Intent intent_HeadIcon = new Intent(MainActivity.this, HeadIcon.class);
                    Bundle bundle = new Bundle();
                    bundle.putString("url", url);
                    intent_HeadIcon.putExtras(bundle);
                    startActivity(intent_HeadIcon);
                } else {
                    Intent intent_HeadIcon = new Intent(MainActivity.this, LoginActivity.class);
                    startActivity(intent_HeadIcon);

                }
                break;
            case R.id.menu_icon_wallet:

                if (flag) {
                    Intent intent_HeadIcon = new Intent(MainActivity.this, MenuWallet.class);
                    startActivity(intent_HeadIcon);

                } else {
                    Intent intent_HeadIcon = new Intent(MainActivity.this, LoginActivity.class);
                    startActivity(intent_HeadIcon);
                }
                break;
            case R.id.menu_icon_coment:
                if (flag) {
                    Intent intent_HeadIcon = new Intent(MainActivity.this, ComentActivity.class);
                    startActivity(intent_HeadIcon);

                } else {
                    Intent intent_HeadIcon = new Intent(MainActivity.this, LoginActivity.class);
                    startActivity(intent_HeadIcon);
                }
                break;
            case R.id.menu_icon_comunity:
                if (flag) {
                    Intent intent_HeadIcon = new Intent(MainActivity.this, ComunityAvtivity.class);
                    startActivity(intent_HeadIcon);

                } else {
                    Intent intent_HeadIcon = new Intent(MainActivity.this, LoginActivity.class);
                    startActivity(intent_HeadIcon);
                }
                break;
            case R.id.menu_icon_fav:
                if (flag) {
                    Intent intent_HeadIcon = new Intent(MainActivity.this, FavActivity.class);
                    startActivity(intent_HeadIcon);

                } else {
                    Intent intent_HeadIcon = new Intent(MainActivity.this, LoginActivity.class);
                    startActivity(intent_HeadIcon);
                }
                break;
            case R.id.menu_icon_geuss:
                if (flag) {
                    Intent intent_HeadIcon = new Intent(MainActivity.this, GeussActivity.class);
                    startActivity(intent_HeadIcon);

                } else {
                    Intent intent_HeadIcon = new Intent(MainActivity.this, LoginActivity.class);
                    startActivity(intent_HeadIcon);
                }
                break;
            case R.id.menu_icon_message:
                if (flag) {
                    Intent intent_Message = new Intent(MainActivity.this, MessageActivity.class);
                    startActivity(intent_Message);

                } else {
                    Intent intent_HeadIcon = new Intent(MainActivity.this, LoginActivity.class);
                    startActivity(intent_HeadIcon);
                }
                break;
            case R.id.menu_icon_task:
                if (flag) {
                    Intent intent_taskActivity = new Intent(MainActivity.this, TaskActivity.class);
                    startActivity(intent_taskActivity);

                } else {
                    Intent intent_HeadIcon = new Intent(MainActivity.this, LoginActivity.class);
                    startActivity(intent_HeadIcon);
                }
                break;
            case R.id.menu_icon_vedio:
                if (flag) {
                    Intent intent_HeadIcon = new Intent(MainActivity.this, VideoThird_downloadActivity.class);
                    startActivity(intent_HeadIcon);

                } else {
                    Intent intent_HeadIcon = new Intent(MainActivity.this, LoginActivity.class);
                    startActivity(intent_HeadIcon);
                }
                break;
            case R.id.gellery_line:
                if (flag) {
                    Intent intent_HeadIcon = new Intent(MainActivity.this, Gellery_line.class);
                    startActivity(intent_HeadIcon);

                } else {
                    Intent intent_HeadIcon = new Intent(MainActivity.this, LoginActivity.class);
                    startActivity(intent_HeadIcon);
                }
                break;
            case R.id.fans_line:
                if (flag) {
                    Intent intent_HeadIcon = new Intent(MainActivity.this, Fans.class);
                    startActivity(intent_HeadIcon);

                } else {
                    Intent intent_HeadIcon = new Intent(MainActivity.this, LoginActivity.class);
                    startActivity(intent_HeadIcon);
                }
                break;
            case R.id.look_line:
                if (flag) {
                    Intent intent_HeadIcon = new Intent(MainActivity.this, Attention.class);
                    startActivity(intent_HeadIcon);

                } else {
                    Intent intent_HeadIcon = new Intent(MainActivity.this, LoginActivity.class);
                    startActivity(intent_HeadIcon);
                }
                break;
            case R.id.dongtai_line:
                if (flag) {
                    Intent intent_HeadIcon = new Intent(MainActivity.this, HeadIcon.class);
                    startActivity(intent_HeadIcon);

                } else {
                    Intent intent_HeadIcon = new Intent(MainActivity.this, LoginActivity.class);
                    startActivity(intent_HeadIcon);
                }
                break;
            case R.id.btn_setting:
            case R.id.tv_setting:
                Intent intent_settting = new Intent(MainActivity.this, SettingActivity.class);
                startActivity(intent_settting);
                break;
            case R.id.btn_go_tou:
                popDialog();
                break;
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void haha(UserInfo user_information) {
        this.user_information = user_information;
        if (user_information != null) {
            this.flag = true;
            this.user_information = user_information;
            Picasso.with(MainActivity.this).load(user_information.getData().getAvatar()).into(head_icon);
            menu_user_name_login.setBackgroundColor(Color.TRANSPARENT);
            menu_user_name_login.setText("小纸呵呵");
            editor.putString("name", user_information.getData().getNickname());
            editor.putString("birthday", user_information.getData().getBirth());
            editor.putString("headIcon", user_information.getData().getAvatar());
            editor.putBoolean("flag", flag);
            editor.commit();
        }
    }

    private void popDialog() {
        final SaveDialog.Builder buider = new SaveDialog.Builder(this);
        buider.setTitle("提示");
        buider.setMessage("是否确认注销当前登录用户？");
        buider.setPositiveButton("取消", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();


            }
        });

        buider.setNegativeButton("确定",
                new android.content.DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {
                        //在此进行资料保存，与底部大Button功能一样
                        dialog.dismiss();
                        finish();
                    }
                });

        buider.create().show();
    }
}

